package cn.waqwb;

import org.apache.hadoop.io.*;

public class HadoopDataType {
    public static void main(String[] args) {
        testText();
        testArrayWritable();
        testMapWritable();
    }

    /**
     * 使用Hadoop的Text类型数据
     */
    public static void testText() {
        System.out.println("============Test Text============");
        Text text = new Text("Hello Hadoop!!!");
        //获取文字的长度
        System.out.println("输入文字的长度是："+text.getLength());
        //获取制定文字的下标位置，从0开始
        System.out.println("字母a出现的下标是："+text.find("a"));
        //变成字符串输出
        System.out.println("输入的字符串是："+text.toString());
    }

    /**
     * 使用ArrayWritable
     */
    public static void testArrayWritable() {
        System.out.println("============Test ArrayWritable============");
        //创建ArrayWritable对象，存入IntWritable类型的值
        ArrayWritable arr = new ArrayWritable(IntWritable.class);
        IntWritable year = new IntWritable(2021);
        IntWritable month = new IntWritable(01);
        IntWritable day = new IntWritable(24);
        //把年月日三个数据加入到数组中
        arr.set(new IntWritable[] {year,month,day});
        //格式化输出，%d为占位符，通过数组的下标获得
        /*
         * System.out.println(String.format("year=%d,month=%d,day=%d",
         * ((IntWritable)(arr.get()[0])).get(), ((IntWritable)(arr.get()[1])).get(),
         * ((IntWritable)(arr.get()[2])).get() ));
         */
        System.out.print("year="+((IntWritable)(arr.get()[0])).get());
        System.out.print(",month="+((IntWritable)(arr.get()[1])).get());
        System.out.println(",day="+((IntWritable)(arr.get()[2])).get());
    }

    /**
     * 使用MapWritable
     */
    public static void testMapWritable() {
        System.out.println("============Test MapWritable============");
        MapWritable map = new MapWritable();
        Text k1 = new Text("name");
        Text v1 = new Text("tonny");
        Text k2 = new Text("password");
        //存入MapWritable,key/value为name/tonny
        map.put(k1,v1);
        //存入MapWritable,key/value为password/null
        map.put(k2, NullWritable.get());
        //通过key获取value值
        System.out.println("name:"+map.get(k1).toString());
        System.out.println("password:"+map.get(k2).toString());
    }
}